package org.ciallo.simplemusicplayer.adapter;

import android.content.Context;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.DiffUtil;
import androidx.recyclerview.widget.ListAdapter;
import androidx.recyclerview.widget.RecyclerView;
import com.bumptech.glide.Glide;
import org.ciallo.simplemusicplayer.R;
import org.ciallo.simplemusicplayer.data.model.Playlist;
import org.ciallo.simplemusicplayer.data.model.PlaylistInfo;
import timber.log.Timber;

import java.util.Locale;
import java.util.Objects;

/**
 * @author Jukomu
 * @Description: 首页显示歌单网格的 RecyclerView 适配器
 * @Package: org.ciallo.simplemusicplayer.adapter
 * @Project: SimpleMusicPlayer
 * @name: PlaylistGridAdapter
 * @Date: 2025/5/30-01:06
 * @Filename: PlaylistGridAdapter
 */
public class PlaylistGridAdapter extends ListAdapter<PlaylistInfo, PlaylistGridAdapter.PlaylistViewHolder> { // ViewHolder类名也改一下

    private final OnPlaylistInfoClickListener listener;

    public interface OnPlaylistInfoClickListener {
        void onPlaylistClick(PlaylistInfo playlistInfo);
    }

    public PlaylistGridAdapter(OnPlaylistInfoClickListener listener) {
        super(DIFF_CALLBACK);
        this.listener = listener;
    }

    private static final DiffUtil.ItemCallback<PlaylistInfo> DIFF_CALLBACK = new DiffUtil.ItemCallback<PlaylistInfo>() {
        @Override
        public boolean areItemsTheSame(@NonNull PlaylistInfo oldItem, @NonNull PlaylistInfo newItem) {
            return oldItem.id == newItem.id;
        }

        @Override
        public boolean areContentsTheSame(@NonNull PlaylistInfo oldItem, @NonNull PlaylistInfo newItem) {
            return oldItem.equals(newItem);
        }
    };

    @NonNull
    @Override
    public PlaylistViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        // 使用新的 item_playlist_row.xml 布局
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_playlist_row, parent, false);
        return new PlaylistViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull PlaylistViewHolder holder, int position) {
        PlaylistInfo playlistInfo = getItem(position);
        if (playlistInfo != null) {
            holder.bind(playlistInfo, listener);
        }
    }

    // ViewHolder 类名可以改为 PlaylistRowViewHolder
    static class PlaylistViewHolder extends RecyclerView.ViewHolder {
        ImageView ivCover;    // 对应 item_playlist_row.xml 中的 ID
        TextView tvName;      // 对应 item_playlist_row.xml 中的 ID
        TextView tvSongCount; // 对应 item_playlist_row.xml 中的 ID

        PlaylistViewHolder(View itemView) {
            super(itemView);
            // 确保这里的 ID 与 item_playlist_row.xml 中的 ID 一致
            ivCover = itemView.findViewById(R.id.imageView_playlist_row_cover);
            tvName = itemView.findViewById(R.id.textView_playlist_row_name);
            tvSongCount = itemView.findViewById(R.id.textView_playlist_row_song_count);
        }

        void bind(final PlaylistInfo playlistInfo, final OnPlaylistInfoClickListener listener) {
            tvName.setText(playlistInfo.name);
            tvSongCount.setText(String.format(Locale.getDefault(), "%d首", playlistInfo.songCount));

            Context context = itemView.getContext();
            Uri coverToLoad = null;
            if (playlistInfo.displayCoverArtUri != null && !playlistInfo.displayCoverArtUri.isEmpty()) {
                try {
                    coverToLoad = Uri.parse(playlistInfo.displayCoverArtUri);
                } catch (Exception e) {
                    Timber.e(e, "Error parsing displayCoverArtUri: %s", playlistInfo.displayCoverArtUri);
                }
            }

            // 根据 playlistType 处理封面
            if (playlistInfo.playlistType == PlaylistInfo.TYPE_FAVORITES_PLAYLIST) {
                Glide.with(context)
                        .load(R.drawable.ic_favorite_filled) // “我的收藏”使用固定的心形图标
                        .placeholder(R.drawable.ic_favorite_filled)
                        .error(R.drawable.ic_favorite_filled)
                        .into(ivCover);
            } else { // 普通歌单
                Glide.with(context)
                        .load(coverToLoad) // 如果 coverToLoad 为 null (歌单为空或第一首歌无封面)
                        .placeholder(R.drawable.default_playlist_cover) // 使用统一的默认歌单封面
                        .error(R.drawable.default_playlist_cover)
                        .into(ivCover);
            }

            itemView.setOnClickListener(v -> {
                if (listener != null) {
                    listener.onPlaylistClick(playlistInfo);
                }
            });
        }
    }
}
